package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.adapter.guang.MyPartInputActivity
import com.c.ctgproject.adapter.guang.PartInWarehouseAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartInputBinding
import com.c.ctgproject.entity.guang.PartsInWarehouseEntity
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.common.CarInfoLayout
import com.c.ctgproject.wiget.dialog.ShowImgVideoFragment
import kotlinx.android.synthetic.main.activity_login.view.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述: 散件录入 页面 (零部件)
 */
class PartInputActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityPartInputBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root

    //如果传值过来了表示 是从我的里面过来的要重新编辑
    private val orderId by lazy { intent.getIntExtra(MyConstants.ORDER_ID, -1).handlerNull() }
//    private var partsId = -1
    override fun event() {
        if (orderId != "-1") {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.blukPartDetail(orderId), MyObserverHandlerLoading(this, loading) {
                MyLog.d("散件入库详情回调:$it")
                val data = getGson().fromJson(it, PartsInWarehouseEntity::class.java).data
                data?.let {
//                    data.partsId?.let { id ->
//                        partsId = id
//                    }
                    mBinding.partsSource.text = data.partsSource.handlerNull()
                    mBinding.unit.text = data.unit.handlerNull()
                    mBinding.partsName.text = data.partsName.handlerNull()
                    mBinding.driverName.textSet(data.driverName.handlerNull())
                    mBinding.driverPlate.textSet(data.driverPlate.handlerNull())
                    mBinding.partsCount.textSet(data.partsCount.handlerNull())
                    mBinding.tollET.textSet(data.toll.handlerNull())
                    mBinding.mRemarkET.textSet(data.remark.handlerNull())
                }
            })
        }
    }

    override fun click() {
        //我录入
        mBinding.mMyInputTV.setOnClickListener {
            startActivity<MyPartInputActivity>()
        }

        //零件选择
        mBinding.mPartSelectLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.PARTS.menuCode, true, arrayListOf(name), partFrom = 1)
        }
        //配件来源
        mBinding.mFromLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.SUPPLIER.menuCode, true, arrayListOf(from), true, 1001)

        }
        //单位
        mBinding.mUnitLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.UNIT.menuCode, true, arrayListOf(unit), true, 1002)
        }


        mBinding.mConfirmBTN.setOnClickListener {
//            val partsSource = mBinding.partsSource.text.handlerNull()
            val driverName = mBinding.driverName.text.handlerNull()
            val driverPlate = mBinding.driverPlate.text.handlerNull()
            val partsCount = mBinding.partsCount.text.handlerNull()
            when {
                from.isEmpty() -> {
                    showToast("请选择零件来源")
                    mBinding.mFromLL.myShake()
                }
                driverName.isEmpty() -> {
                    showToast("请输入司机姓名")
                    mBinding.mDriverNameLL.myShake()
                }
                driverPlate.isEmpty() -> {
                    showToast("请输入司机车牌")
                    mBinding.mPlateLL.myShake()
                }
                name.isEmpty() -> {
                    showToast("请选择零件")
                    mBinding.mPartSelectLL.myShake()
                }
                partsCount.isEmpty() -> {
                    showToast("请输入零件数量")
                    mBinding.mCountLL.myShake()
                }
                unit.isEmpty() -> {
                    showToast("请选择零件单位")
                    mBinding.mUnitLL.myShake()
                }
//                mBinding.tollET.text.handlerNull().isEmpty() -> showToast("请输入路桥费")
                else -> {
                    val map = HashMap<String, Any>()
                    map["partsSource"] = mBinding.partsSource.text.handlerNull()
                    map["driverName"] = mBinding.driverName.text.handlerNull()
                    map["driverPlate"] = mBinding.driverPlate.text.handlerNull()
                    map["partsName"] = name
                    map["partsAbbreviation"] = abb
                    map["partsCount"] = mBinding.partsCount.text.handlerNull()
                    map["unit"] = mBinding.unit.text.handlerNull()
                    map["toll"] = mBinding.tollET.text.handlerNull()
                    map["remark"] = mBinding.mRemarkET.text.handlerNull()
                    if (orderId != "-1") {
                        map["orderId"]=orderId
//                        map["partsId"]=partsId
                        //我的录入重新提交
                        ApiMethods.apiSubscribe(ApiMethods.retrofit.partInputUpdate(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                            showToast("提交成功")
                            finish()
                        })
                    } else {
                        ApiMethods.apiSubscribe(ApiMethods.retrofit.partInput(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                            showToast("录入成功")
                            finish()
                        })
                    }
                }
            }
        }
    }

    var name = ""
    var abb = ""
    var from = ""
    var unit = ""
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            if (requestCode == 1000) {
                name = data.getStringExtra(MyConstants.NAME).handlerNull()
                abb = data.getStringExtra(MyConstants.ABBREVIATION).handlerNull()
                mBinding.partsName.text = name
            }
            if (requestCode == 1001) {
                val name = data.getStringExtra(MyConstants.NAME)
                from = name.handlerNull()
                mBinding.partsSource.text = name.handlerNull()
            }
            if (requestCode == 1002) {
                val name = data.getStringExtra(MyConstants.NAME)
                unit = name.handlerNull()
                mBinding.unit.text = name.handlerNull()
            }
        }
    }
}